program BGK

  use fft_wrap
  use mpi_wrap
!  use efield
!  use evolve

  use dimensions

  use parameters, only : restart, nt, dt, iDiag, iData, iLinVlasov, runname

  implicit none

  !-- major arrays ---------------------


  complex    :: dfu(jv, jz, jy, jx, jDir)       ! distribution function
  complex    :: uxMat(0, 0, 0, 0)               ! velocity, x-component, unused
  complex    :: uyMat(0, 0, 0, 0)               ! velocity, y-component, unused
  complex    :: Ez2D(jz, jy, jx)                ! electric field, z-component

  real       :: Fbg(jv)                         ! f_bg


  !-- variables  ----------------------- 

  real       :: t                               ! time
  integer    :: it                              ! iteration count
  integer    :: iargc                           ! argument count


 
 
!-------------------------------------------------------------------------------------

      !--  reading the run_name from the command line

      if(iargc().eq.0) then
         call getarg(0,runname)
         write(*,*)      'Usage: ',trim(runname),' <run name>'
         call mpi_abort(MPI_COMM_WORLD, -1, mpi_err)
         stop
      end if
      call getarg(1,runname)


      !-- set parameters and coordinate space --

      call mpi_wrap_init()

      call read_input()

      call fft_init(dfu)

      call set_space_vectors()


      !-- restore from restart file or create initial conditions --

      call maxwell(Fbg)

      call set_ic_nlvp(dfu, Fbg, it)

      t  = it*dt


      !-- save initial conditions --

      if (.not. restart) then

         call evolve_vp_efield( dfu, Ez2D)

         call diagnostics_nlvp( dfu, Ez2D, t)
         write(msg, "(' Diagnostcs, t =' e12.6)") t
         call mpi_wrap_msg


         call write_bin(dfu, uxMat, uyMat, it, it/iData)
         write(msg, "(' Output ' i4.4)") it/iData
         call mpi_wrap_msg
 
      end if



      write(msg,*) char(10), ' Begin temporal evolution', char(10)
      call mpi_wrap_msg


!-------------------------------------------------------------------------------------
! BEGIN EVOLUTION
!-------------------------------------------------------------------------------------

  call evolve_vp_advect( dfu, dt/2)                      !A/2

  do while (it .le. nt) 

    call dealias_5D(dfu)

    call evolve_vp_efield( dfu, Ez2D)                    !C

    if (iLinVlasov) then
       call evolve_vp_accel_lin( dfu, Fbg, Ez2D, dt)     !B
    else
       call evolve_vp_accel( dfu, Ez2D, dt)
    endif

    call evolve_vp_filter(dfu)

    t  = t + dt
    it = it + 1

    if  ( (it/iDiag)*iDiag.eq.it ) then

       call evolve_vp_advect( dfu, dt/2)

       call evolve_vp_efield( dfu, Ez2D)

       call diagnostics_nlvp( dfu, Ez2D, t)
       write(msg, "(' Diagnostcs, t =' e12.6)") t
       call mpi_wrap_msg

       if ( (it/iData)*iData.eq.it ) then
          call write_bin(dfu, uxMat, uyMat, it, it/iData)
          write(msg, "(' Output ' i4.4)") it/iData
          call mpi_wrap_msg
       endif

       call evolve_vp_advect( dfu, dt/2)

    else

       call evolve_vp_advect( dfu, dt )                    !A

    end if

  end do      ! END TEMPORAL LOOP
 

  write(msg,*) char(10), ' End temporal evolution', char(10)
  call mpi_wrap_msg

  !-- write some specrtral spectrum quantities --
       
  call mpi_wrap_exit()
    
  stop;

end


!-------------------------------------------------------------------------------------
subroutine maxwell(Fbg)
!-------------------------------------------------------------------------------------

  use dimensions
  use parameters, only  :  runname, Vmax, pi

  implicit none

  real, intent(out)       :: Fbg(jv)                     ! f_bg

  real                    :: f0
  integer                 :: iv


  ! reminder:  vvec(iv) = -vPhase*iWaveFrame - Vmax + dv*(iv-1)
  ! reminder:  f0vec(:) = ( 1/sqrt(2*pi) )*exp(-0.5*(vvec(:) + vPhase*iWaveFrame)**2)

  do iv = 1,jv
     f0 = ( 1/sqrt(2*pi) )*exp(-0.5*(-Vmax + dv*(iv-1))**2)
     Fbg(iv) = f0
     !dFbg(iv) =  -(vvec(iv) + vPhase*iWaveFrame)*f0
  end do

end subroutine maxwell


!-------------------------------------------------------------------------------------
